package com.mj.haier.calc.servlet.impl;

import java.io.IOException;
import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mj.haier.calc.servlet.Filter;
import com.mj.haier.calc.servlet.FilterChain;
import com.mj.haier.calc.servlet.Request;
import com.mj.haier.calc.servlet.Response;
import com.mj.haier.calc.servlet.ServletException;
import com.mj.haier.calc.domain.AzEorderSettlementProcess;

public class PercentFilter implements Filter {


    private final Logger log = LoggerFactory.getLogger(PercentFilter.class);

    @Override
    public void doFilter(Request req, Response res, FilterChain chain) throws IOException, ServletException {
        
        AzEorderSettlementProcess process = (AzEorderSettlementProcess)req.getObject();
        log.debug("CarsFilter 过滤前 AzEorderSettlementProcess: {}", process.toString());

        if("0".equals(process.getPercentCompliance())){
            chain.doFilter(req, res);
            return;
        }

        // 合规性与计算费用
        String compliance = "0";
        StringBuilder complianceSb = new StringBuilder();
        String carPercent = process.getCarPercent();
        String carteamPercent = process.getCarsteamPercent();
        if(StringUtils.isAnyBlank(carPercent, carteamPercent)){
            compliance = "1";
            log.debug("订单上的车辆分成比：{}, 车队分成比：{}, 不合规", carPercent, carteamPercent);
            complianceSb.append("订单上的车辆分成比:"+carPercent+", 车队分成比："+carteamPercent+", 不合规").append("&");
        }
        if("0".equals(compliance)){
            if((Integer.parseInt(carPercent) + Integer.parseInt(carteamPercent)) == 100){

                BigDecimal carTeamPercentBigDecimal = new BigDecimal(carteamPercent).divide(new BigDecimal("100"));
                BigDecimal totalFee = new BigDecimal(nullToZero(process.getFeeTotalCost()));
                BigDecimal carsteamTotalFee = totalFee.multiply(carTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsTotalFee = totalFee.subtract(carsteamTotalFee);

                /**
                 * 分别计算相应明细金额的分成比费用
                 */
                BigDecimal rewardCost = new BigDecimal(nullToZero(process.getRewardCost()));
                BigDecimal carsteamRewardCost = rewardCost.multiply(carTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsRewardCost = rewardCost.subtract(carsteamRewardCost);

                BigDecimal compensatoryCost = new BigDecimal(nullToZero(process.getCompensatoryCost()));
                BigDecimal carsteamCompensatoryCost = compensatoryCost.multiply(carTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsCompensatoryCost = compensatoryCost.subtract(carsteamCompensatoryCost);

                BigDecimal SubidyCost = new BigDecimal(nullToZero(process.getSubidyCost()));
                BigDecimal carsteamSubidyCost = SubidyCost.multiply(carTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsSubidyCost = SubidyCost.subtract(carsteamSubidyCost);

                BigDecimal MeterialCost = new BigDecimal(nullToZero(process.getMeterialCost()));
                BigDecimal carsteamMeterialCost = MeterialCost.multiply(carTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsMeterialCost = MeterialCost.subtract(carsteamMeterialCost);

                BigDecimal TrafficCost = new BigDecimal(nullToZero(process.getTrafficCost()));
                BigDecimal carsteamTrafficCost = TrafficCost.multiply(carTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsTrafficCost = TrafficCost.subtract(carsteamTrafficCost);

                BigDecimal ServiceTypeCost = new BigDecimal(nullToZero(process.getServiceTypeCost()));
                BigDecimal carsteamServiceTypeCost = ServiceTypeCost.multiply(carTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsServiceTypeCost = ServiceTypeCost.subtract(carsteamServiceTypeCost);

                BigDecimal carsteamOthers = carsteamRewardCost.add(carsteamCompensatoryCost).add(carsteamSubidyCost).add(carsteamMeterialCost).add(carsteamTrafficCost).add(carsteamServiceTypeCost);
                BigDecimal carsOthers = carsRewardCost.add(carsCompensatoryCost).add(carsSubidyCost).add(carsMeterialCost).add(carsTrafficCost).add(carsServiceTypeCost);

                BigDecimal carsteamOperateCost = carsteamTotalFee.subtract(carsteamOthers);
                BigDecimal carsOperateCost = carsTotalFee.subtract(carsOthers);


                complianceSb.append("按照订单上的分成比合规并计算金额").append("&");
                process.carsTotalFee(carsTotalFee.toPlainString())
                    .carsTeamTotalFee(carsteamTotalFee.toPlainString())
                    .percentCompliance(compliance)
                    .percentMsg(complianceSb.toString())
                    .carsTeamRewardCost(carsteamRewardCost.toPlainString())
                    .carsRewardCost(carsRewardCost.toPlainString())
                    .carsTeamCompensatoryCost(carsteamCompensatoryCost.toPlainString())
                    .carsCompensatoryCost(carsCompensatoryCost.toPlainString())
                    .carsTeamSubidyCost(carsteamSubidyCost.toPlainString())
                    .carsSubidyCost(carsSubidyCost.toPlainString())
                    .carsTeamMeterialCost(carsteamMeterialCost.toPlainString())
                    .carsMeterialCost(carsMeterialCost.toPlainString())
                    .carsTeamServiceTypeCost(carsteamServiceTypeCost.toPlainString())
                    .carsServiceTypeCost(carsServiceTypeCost.toPlainString())
                    .carsTeamTrafficCost(carsteamTrafficCost.toPlainString())
                    .carsTrafficCost(carsTrafficCost.toPlainString())
                    .carsTeamOperateCost(carsteamOperateCost.toPlainString())
                    .carsOperateCost(carsOperateCost.toPlainString());


                log.debug("CarsFilter 过滤前 AzEorderSettlementProcess: {}", process.toString());
                chain.doFilter(req, res);
                return;
            }else{
                compliance = "1";
                complianceSb.append("订单上的车辆分成比:"+carPercent+", 车队分成比："+carteamPercent+", 相加不等于100， 规则不合规").append("&");
            }
        }

        String carsInfoCarPercent = process.getCarsInfoCarsPercent().toString();
        String carsInfoCarteamPercent = process.getCarsInfoCarsTeamPercent().toString();

        if(!StringUtils.isAnyBlank(carsInfoCarPercent, carsInfoCarteamPercent)){
            // compliance = "1";
            log.debug("车辆上的车辆分成比：{}, 车队分成比：{}, 不合规", carsInfoCarPercent, carsInfoCarteamPercent);
            
            if((Integer.parseInt(carsInfoCarPercent) + Integer.parseInt(carsInfoCarteamPercent)) == 100){
                compliance = "0";
                BigDecimal carsInfoCarTeamPercentBigDecimal = new BigDecimal(carsInfoCarteamPercent).divide(new BigDecimal("100"));

                BigDecimal totalFee = new BigDecimal(nullToZero(process.getFeeTotalCost()));
                BigDecimal carsteamTotalFee = totalFee.divide(carsInfoCarTeamPercentBigDecimal, 2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsTotalFee = totalFee.subtract(carsteamTotalFee);

                /**
                 * 分别计算相应明细金额的分成比费用
                 */
                BigDecimal rewardCost = new BigDecimal(nullToZero(process.getRewardCost()));
                BigDecimal carsteamRewardCost = rewardCost.multiply(carsInfoCarTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsRewardCost = rewardCost.subtract(carsteamRewardCost);

                BigDecimal compensatoryCost = new BigDecimal(nullToZero(process.getCompensatoryCost()));
                BigDecimal carsteamCompensatoryCost = compensatoryCost.multiply(carsInfoCarTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsCompensatoryCost = compensatoryCost.subtract(carsteamCompensatoryCost);

                BigDecimal SubidyCost = new BigDecimal(nullToZero(process.getSubidyCost()));
                BigDecimal carsteamSubidyCost = SubidyCost.multiply(carsInfoCarTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsSubidyCost = SubidyCost.subtract(carsteamSubidyCost);

                BigDecimal MeterialCost = new BigDecimal(nullToZero(process.getMeterialCost()));
                BigDecimal carsteamMeterialCost = MeterialCost.multiply(carsInfoCarTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsMeterialCost = MeterialCost.subtract(carsteamMeterialCost);

                BigDecimal TrafficCost = new BigDecimal(nullToZero(process.getTrafficCost()));
                BigDecimal carsteamTrafficCost = TrafficCost.multiply(carsInfoCarTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsTrafficCost = TrafficCost.subtract(carsteamTrafficCost);

                BigDecimal ServiceTypeCost = new BigDecimal(nullToZero(process.getServiceTypeCost()));
                BigDecimal carsteamServiceTypeCost = ServiceTypeCost.multiply(carsInfoCarTeamPercentBigDecimal).setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal carsServiceTypeCost = ServiceTypeCost.subtract(carsteamServiceTypeCost);

                BigDecimal carsteamOthers = carsteamRewardCost.add(carsteamCompensatoryCost).add(carsteamSubidyCost).add(carsteamMeterialCost).add(carsteamTrafficCost).add(carsteamServiceTypeCost);
                BigDecimal carsOthers = carsRewardCost.add(carsCompensatoryCost).add(carsSubidyCost).add(carsMeterialCost).add(carsTrafficCost).add(carsServiceTypeCost);

                BigDecimal carsteamOperateCost = carsteamTotalFee.subtract(carsteamOthers);
                BigDecimal carsOperateCost = carsTotalFee.subtract(carsOthers);

                complianceSb.append("按照车辆上的分成比合规并计算金额").append("&");
                process.carsTotalFee(carsTotalFee.toPlainString())
                    .carsTeamTotalFee(carsteamTotalFee.toPlainString())
                    .percentCompliance(compliance)
                    .percentMsg(complianceSb.toString())
                    .carsTeamRewardCost(carsteamRewardCost.toPlainString())
                    .carsRewardCost(carsRewardCost.toPlainString())
                    .carsTeamCompensatoryCost(carsteamCompensatoryCost.toPlainString())
                    .carsCompensatoryCost(carsCompensatoryCost.toPlainString())
                    .carsTeamSubidyCost(carsteamSubidyCost.toPlainString())
                    .carsSubidyCost(carsSubidyCost.toPlainString())
                    .carsTeamMeterialCost(carsteamMeterialCost.toPlainString())
                    .carsMeterialCost(carsMeterialCost.toPlainString())
                    .carsTeamServiceTypeCost(carsteamServiceTypeCost.toPlainString())
                    .carsServiceTypeCost(carsServiceTypeCost.toPlainString())
                    .carsTeamTrafficCost(carsteamTrafficCost.toPlainString())
                    .carsTrafficCost(carsTrafficCost.toPlainString())
                    .carsTeamOperateCost(carsteamOperateCost.toPlainString())
                    .carsOperateCost(carsOperateCost.toPlainString());
                    
                log.debug("CarsFilter 过滤前 AzEorderSettlementProcess: {}", process.toString());
                chain.doFilter(req, res);
                return;
            }
        }

        complianceSb.append("车辆上的车辆分成比:"+carsInfoCarPercent+", 车队分成比："+carsInfoCarteamPercent+", 不合规").append("&");
        process.percentCompliance(compliance)
            .percentMsg(complianceSb.toString());
        chain.doFilter(req, res);
    }

    private String nullToZero(String num){
        return null == num? "0": num;
    }
}